import {doDelete, doGet, doPost, doPut} from './correspondence.js'

/**
 * 发送注册验证码邮件
 * @param email
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function sendCaptchaToEmail(email) {
    return doPost('/user/captcha', {email}).then(res => res)
}

/**
 * 验证当前注册邮箱是否已被注册
 * @param email
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function checkEmailValid(email) {
    return doPost('/user/check-email', {email}).then(res => res)
}

/**
 * 注册
 * @param email
 * @param password
 * @param captcha
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function register123(email, password, captcha) {
    return doPost(
      '/user/register',
      {email, password, captcha}
    ).then(res => res)
}

/**
 * 登录
 * @param email
 * @param password
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function login123(email, password) {
    return doPost(
      '/user/login',
      {email, password}
    ).then(res => res)
}

/**
 * 获取用户信息
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getUserInfo() {
    return doGet('/user/info').then(res => res)
}

/**
 * 修改昵称
 * @param name
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setUserName(name) {
    return doPut(
      '/user/name',
      {name}
    ).then(res => res)
}

/**
 * 上传头像
 * @param avatar
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function uploadAvatar(avatar) {
    const formData = new FormData()
    formData.append('avatar', avatar, avatar.name)
    return doPut('/user/avatar', formData, {
          headers: {
              'accept': 'application/json',
              'Content-Type': `multipart/form-data; boundary=${formData._boundary}`
          }
    })
}

/**
 * 获取网址捷径（并排序）
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getShortcuts() {
    return doGet('/shortcut/entities').then(res => {
        if (res.retData instanceof Array) {
            res.retData.sort((a, b) => a.position - b.position)
        }
        return res
    })
}

/**
 * 添加网址捷径
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function addShortcut(title, url, ico = null) {
    return doPost('/shortcut/entity', {title, url, ico}).then(res => res)
}

/**
 * 根据id删除网址捷径
 * @param id
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function deleteShortcut(id) {
    return doDelete(`/shortcut/entity/${id}`).then(res => res)
}

/**
 * 根据id获取网址捷径
 * @param id
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getShortcut(id) {
    return doGet(`/shortcut/entity/${id}`).then(res => res)
}

/**
 * 移动网址捷径位置
 * @returns {Promise<axios.AxiosResponse<any>>}
 * @param from 从哪里
 * @param to 到哪里
 */
export function sortShortcuts(from, to) {
    return doPut('/shortcut/position', {from, to}).then(res => res)
}

/**
 * 获取用户所有设置
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getSetting() {
    return doGet('/setting/all').then(res => res)
}

/**
 * 设置是否显示分享句子
 * @param newValue
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setShareSentence(newValue) {
    return doPut('/setting/share-sentence', {newValue}).then(res => res)
}

/**
 * 设置是否显示今日天气
 * @param newValue
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setShowWeather(newValue) {
    return doPut('/setting/show-weather', {newValue}).then(res => res)
}

/**
 * 设置时钟类型
 * @param newValue
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setTimeType(newValue) {
    return doPut('/setting/time-type', {newValue}).then(res => res)
}

/**
 * 设置搜索结果附加功能
 * @param newValue
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setSearchResultsOption(newValue) {
    return doPut('/setting/search-results-option', {newValue}).then(res => res)
}

/**
 * 设置当前壁纸
 * @param newValue
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function setCurrentWallpaper(newValue) {
    return doPut('/setting/current-wallpaper', {newValue}).then(res => res)
}

/**
 * 获取爱奇艺热搜
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getIqyHotSearch() {
    return doGet('/remote/hot-search/iqy').then(res => res)
}

/**
 * 获取抖音热搜
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getDouyinHotSearch() {
    return doGet('/remote/hot-search/douyin').then(res => res)
}

/**
 * 获取哔哩哔哩热搜
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getBilibiliHotSearch() {
    return doGet('/remote/hot-search/bilibili').then(res => res)
}

/**
 * 获取新浪微博热搜
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getWeiboHotSearch() {
    return doGet('/remote/hot-search/weibo').then(res => res)
}

/**
 * 上传壁纸
 * @param file
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function uploadCustomWallpaper(file) {
    const formData = new FormData()
    formData.append('file', file, file.name)
    return doPut('/setting/wallpaper', formData, {
        headers: {
            'accept': 'application/json',
            'Content-Type': `multipart/form-data; boundary=${formData._boundary}`
        }
    })
}

/**
 * 获取自定义壁纸
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function getCustomWallpapers() {
    return doGet('/setting/wallpapers').then(res => res)
}

/**
 * 根据id删除自定义壁纸
 * @param id
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function deleteCustomWallpaper(id) {
    return doDelete(`/setting/wallpaper/${id}`).then(res => res)
}

export async function fetchAllUserinfo() {
    return await Promise.allSettled([
        getUserInfo(),
        getSetting(),
        getShortcuts(),
        getCustomWallpapers()
    ])
}

/**
 * 发送忘记密码邮件
 * @param email
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function sendForgetPasswordCaptcha(email) {
    return doPost(`/user/forget-password/captcha`, {email}).then(res => res)
}

/**
 * 重置密码
 * @param email
 * @param password
 * @param captcha
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export function resetPassword(email, password, captcha) {
    return doPut(`/user/forget-password/reset`, {email, password, captcha}).then(res => res)
}
